package com.zhsq.zlc.controller.role;

import com.zhsq.zlc.entity.*;
import com.zhsq.zlc.service.authority.AuthorityService;
import com.zhsq.zlc.service.log.LogService;
import com.zhsq.zlc.service.role.RoleService;
import com.zhsq.zlc.util.Constant;
import com.zhsq.zlc.util.MenuUtil;
import com.zhsq.zlc.util.PageUtil;
import com.zhsq.zlc.util.SessionInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class RoleController {

	@Autowired
	private RoleService roleService;

	@Autowired
	private AuthorityService authorityService;

	@Autowired
	private LogService logService;
	/**
	 * 角色列表
	 * @param entity
	 * @param pageCurrent
	 * @param pageSize
	 * @param pageCount
	 * @param model
	 * @return
	 */
	@RequestMapping("/zhsq/roleManage_{pageCurrent}_{pageSize}_{pageCount}")
	public String roleManage(RoleEntity entity, @PathVariable Integer pageCurrent, @PathVariable Integer pageSize, @PathVariable Integer pageCount, Model model) throws Exception{
		//判断
		if(pageSize == 0) pageSize = 10;
		if(pageCurrent == 0) pageCurrent = 1;
		int rows = roleService.count(entity);
		if(pageCount == 0) pageCount = rows%pageSize == 0 ? (rows/pageSize) : (rows/pageSize) + 1;

		//查询
		entity.setStart((pageCurrent - 1)*pageSize);
		entity.setEnd(pageSize);
		List<RoleEntity> list = roleService.list(entity);
		if(list!=null && list.size()>0){
			for (int i=0;i<list.size();i++){
				list.get(i).setDataIndex(entity.getStart()+i+1);
			}
		}
		//输出
		model.addAttribute("list", list);
		String pageHTML = PageUtil.getPageContent("roleManage_{pageCurrent}_{pageSize}_{pageCount}?name="+entity.getName(), pageCurrent, pageSize, pageCount);
		model.addAttribute("pageHTML",pageHTML);
		model.addAttribute("role",entity);
		return "zhsq/roleManage";
	}

	/**
	 * 角色新增、修改跳转
	 * @param model
	 * @param entity
	 * @return
	 */
	@PostMapping("/zhsq/roleEditGetEntity")
	public String roleEditGetEntity(Model model,RoleEntity entity) throws Exception{
		if(entity.getId()!=null){
			model.addAttribute("role",roleService.getRoleById(entity));
		}else{
			model.addAttribute("role",new RoleEntity());
		}
		return "zhsq/roleEdit";
	}

	/**
	 * 角色新增、修改提交
	 * @param model
	 * @param entity
	 * @param httpSession
	 * @return
	 */
	@PostMapping("/zhsq/roleEdit")
	public String roleEditPost(Model model, RoleEntity entity, HttpSession httpSession, HttpServletRequest request)throws Exception {
		HttpSession session = request.getSession();
		SessionInfo sessioninfo = (SessionInfo)session.getAttribute("sessionInfo");
		if(entity.getId()!=null){
			roleService.edit(entity);
			this.logService.insertLog(LogEntity.EDIT_TYPE, "修改角色数据"+entity.toString(), sessioninfo.getUser().getLoginName(), true);
		} else {
			roleService.add(entity);
			this.logService.insertLog(LogEntity.INSERT_TYPE, "添加角色数据"+entity.toString(), sessioninfo.getUser().getLoginName(), true);
		}
		return "redirect:roleManage_0_0_0";
	}

	/**
	 * 角色配置权限
	 * @param model
	 * @param entity
	 * @return
	 */
	@PostMapping("/zhsq/roleConfigAuthorityGetEntity")
	public String roleConfigAuthorityGet(Model model,RoleEntity entity) throws Exception{
		if(entity.getId()!=null){
			model.addAttribute("role",roleService.getRoleById(entity));
		}
		return "zhsq/roleConfigAuthority";
	}

	/**
	 * 根据权限ID获取父节点权限
	 * @param entity
	 * @param request
	 * @return
	 */
	@ResponseBody
	@PostMapping("/zhsq/getParentAuthorityById")
	public ResultObject<Object> getParentAuthorityById(AuthorityEntity entity, HttpServletRequest request) throws Exception{
		ResultObject<Object> object = new ResultObject<Object>(Constant.Code01, Constant.Msg01,authorityService.getParentAuthorityById(entity.getId()), null);
		return object;
	}

	/**
	 * 获取权限树
	 * @param entity
	 * @param request
	 * @return
	 */
	@ResponseBody
	@PostMapping("/zhsq/getAuthorityTree")
	public ResultObject<Object> getAuthorityTree(IDEntity entity, HttpServletRequest request) throws Exception{
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("tree",authorityService.getAuthorityTree());
		map.put("authoritys",authorityService.getAuthorityByRoleId(entity.getIntID()));
		ResultObject<Object> object = new ResultObject<Object>(Constant.Code01, Constant.Msg01,map, null);
		return object;
	}
	/**
	 * 保存权限树
	 * @param entity
	 * @return
	 */
	@PostMapping("/zhsq/savaRoleAuthoritys")
	public String savaRoleAuthoritys(RoleAuthoritySaveEntity entity, HttpServletRequest request) throws Exception{
		// 先清除所有权限
		authorityService.delAuthorityByRole(entity.getRoleId());
		// 保存最新的权限
		if (entity.getAuthorityIds() != null && !"".equals(entity.getAuthorityIds().trim())) {
			authorityService.batchInsertRoleAuthority(entity.getRoleId(), entity.getAuthorityIds());
		}
		//刷新权限
		HttpSession session = request.getSession();
		SessionInfo sessioninfo = (SessionInfo)session.getAttribute("sessionInfo");
		sessioninfo.setUserAuthority(authorityService.getAuthorityTreeByRoleId(sessioninfo.getUser().getRoleId()));
		sessioninfo.setUserAuthorityNavHtml(MenuUtil.getNavMenu(sessioninfo.getUserAuthority()));
		request.getSession().setAttribute("sessionInfo", sessioninfo);
		this.logService.insertLog(LogEntity.INSERT_TYPE, "角色配置权限"+entity.toString(), sessioninfo.getUser().getLoginName(), true);
		ResultObject<Object> object = new ResultObject<Object>(Constant.Code01, Constant.Msg01,null, null);
		return "redirect:roleManage_0_0_0";
	}

	/**
	 * 删除权限树
	 * @param entity
	 * @return
	 */
	@ResponseBody
	@PostMapping("/zhsq/roleDelete")
	public ResultObject<Object> roleDelete(IDEntity entity, HttpServletRequest request) throws Exception{
		HttpSession session = request.getSession();
		SessionInfo sessioninfo = (SessionInfo)session.getAttribute("sessionInfo");
		roleService.delete(entity.getIntID());
		authorityService.delAuthorityByRole(entity.getIntID());
		this.logService.insertLog(LogEntity.DELETE_TYPE, "删除角色数据"+entity.toString(), sessioninfo.getUser().getLoginName(), true);
		ResultObject<Object> object = new ResultObject<Object>(Constant.Code01, Constant.Msg01,null, null);
		return object;
	}
}
